

set more off
mat drop _all


use "$path_data/data/temp/attrition_check.dta", clear








gen miss_cpcs = 0
replace miss_cpcs = 1 if missing_in_ptsbl_2 == 1 & ///
missing_in_ptsbl_3 == 1 & ///
missing_in_ptsbl_4  == 1 & ///
missing_in_ptsbl_5  == 1 & ///
missing_in_ptsbl_6  == 1 & ///
missing_in_ptsbl_7  == 1 & ///
missing_in_ptsbl_8  == 1 & ///
missing_in_ptsbl_9  == 1 & ///
missing_in_ptsbl_10  == 1 & ///
missing_in_ptsbl_11 
gen miss_rosen = 0
replace miss_rosen = 1 if missing_in_ptsbl_2  == 1 & ///
missing_in_ptsbl_3  == 1 & ///
missing_in_ptsbl_4  == 1 & ///
missing_in_ptsbl_6  == 1 & ///
missing_in_ptsbl_8  == 1 & ///
missing_in_ptsbl_9  == 1 & ///
missing_in_ptsbl_10 == 1 & ///
missing_in_ptsbl_11 








*** Test for DT
replace DT_per_min_pre = . if wrongDT == 1
replace DT_time_pre = . if wrongDT == 1
replace DT_score_pre = . if wrongDT == 1


rename rosen_pre modi_rosen_pre
rename rosen_bl rosen_pre
rename cpcs_pre modi_cpcs_pre
rename cpcs_bl cpcs_pre


rename rosen_post modi_rosen_post
rename rosen_el rosen_post
rename cpcs_post modi_cpcs_post
rename cpcs_el cpcs_post




















	
	
	
	*** ATtrition in ANCOVA
	
	
foreach j in DT_score DT_time DT_per_min pts ///
rosen cpcs {
	 gen att_`j'_post = 0
	 replace att_`j'_post = 1 if `j'_post == .
	
}

	



	
* Only DT is not balacing, so deal with it only.
gen missing_DT = 0
replace missing_DT = 1 if DT_per_min_pre == .
egen mean_DT_per_min_pre = mean(DT_per_min_pre)
replace DT_per_min_pre = mean_DT_per_min_pre if DT_per_min_pre == .
egen mean_DT_time_pre = mean(DT_time_pre)
replace DT_time_pre = mean_DT_time_pre if DT_per_min_pre  == .
egen mean_DT_score_pre = mean(DT_score_pre)
replace DT_score_pre = mean_DT_score_pre if DT_per_min_pre  == .






*foreach i in DT_score_post DT_time_post DT_per_min_post ptsel_overall_score ///
*rosen_el cpcs_el {




foreach j in DT_score DT_time DT_per_min pts ///
rosen cpcs {

*	di ""
*	di ""
*	di ""
*	di ""
*	di ""
*	di "Cluster reg of baseline characteristics with respect to endline attrition status"

	
*	foreach j in DT_score_pre DT_time_pre DT_per_min_pre ptsbl_overall_score ///
*	rosen_bl cpcs_bl {


	foreach s in mean se pv obs {
		matrix Att_`j'_post_`s' = J(1,3,.)
	}
	
	
	
	
	*** Reg of attririon status on treatment dummy
	 reg att_`j'_post treatment, cluster(school_no)

		
	matrix Att_`j'_post_temp = r(table)
	matrix Att_`j'_post_r2 = e(r2)
	matrix Att_`j'_post_obs = e(N)

	 cgmwildboot_2 att_`j'_post treatment, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_post_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_post_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_post_mean[1,`i'] = Att_`j'_post_temp[1,`i']
		* standard error
		matrix Att_`j'_post_se[1,`i'] = Att_`j'_post_temp[2,`i']
		* p value
		matrix Att_`j'_post_pv[1,`i'] = Att_`j'_post_temp[4,`i']
		matrix Att_`j'_post_wpv[1,`i'] = Att_`j'_post_temp_p[`i',1]
	}

	

	
	
		** Reg of baseline-outcome on treatment for attritors
	
*		di ""
*		di ""
*		di ""
*		di "Cluster reg of baseline `j' with respect to the attrition status on endline `j'"
	 reg `j'_pre treatment if att_`j'_post == 1, cluster(school_no)

		
	matrix Att_`j'_pre_temp = r(table)
	matrix Att_`j'_pre_r2 = e(r2)
	matrix Att_`j'_pre_obs = e(N)

	 cgmwildboot_2 `j'_pre treatment if att_`j'_post == 1, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_pre_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_pre_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_pre_mean[1,`i'] = Att_`j'_pre_temp[1,`i']
		* standard error
		matrix Att_`j'_pre_se[1,`i'] = Att_`j'_pre_temp[2,`i']
		* p value
		matrix Att_`j'_pre_pv[1,`i'] = Att_`j'_pre_temp[4,`i']
		matrix Att_`j'_pre_wpv[1,`i'] = Att_`j'_pre_temp_p[`i',1]
	}

	
}
	

	
	
	
	
	
	
	
	
	

*run this after "attrition_status.do"




tempname hh
file open `hh' using "$pardir/tableD3.tex", write replace
file write `hh' "" _newline
file write `hh' "% Date: `c(current_date)'" _newline
file write `hh' "% Time: `c(current_time)'" _newline
file write `hh' "" _newline


file write `hh' "\begin{table}[t!]\footnotesize" _newline
file write `hh' "  \centering" _newline
file write `hh' "  \caption{Attrition Status}" _newline
file write `hh' "\label{tab:attrition}" _newline
file write `hh' "\scalebox{0.85}{" _newline
file write `hh' "\begin{threeparttable}" _newline
file write `hh' "\begin{tabular}{lccc}\toprule" _newline



***************      AttA1_`i'_pre_pv


foreach j in DT_score DT_time DT_per_min pts ///
rosen cpcs {
foreach p in pre post {
forvalues item = 1/6 {
		forvalues scale = 1/1 {
		if Att_`j'_`p'_wpv[`item', `scale']<=0.01 {
			local star_`j'_`p'_`item'`scale' %3s "***"
		}
		else if (Att_`j'_`p'_wpv[`item', `scale']>0.01) & (Att_`j'_`p'_wpv[`item', `scale']<=0.05) {
			local star_`j'_`p'_`item'`scale' %2s "**"
		}
		else if (Att_`j'_`p'_wpv[`item', `scale']>0.05) & (Att_`j'_`p'_wpv[`item', `scale']<=0.10) {
			local star_`j'_`p'_`item'`scale' %1s "*"
		}
		else {
			local star_`j'_`p'_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop
}  // foreach `p' loop


} // forvalues `j' loop



foreach j in DT_score DT_time DT_per_min pts ///
rosen cpcs {
foreach p in pre post {
forvalues item = 1/6 {
		forvalues scale = 2/2 {
		if Att_`j'_`p'_pv[`item', `scale']<=0.01 {
			local star_`j'_`p'_`item'`scale' %3s "***"
		}
		else if (Att_`j'_`p'_pv[`item', `scale']>0.01) & (Att_`j'_`p'_pv[`item', `scale']<=0.05) {
			local star_`j'_`p'_`item'`scale' %2s "**"
		}
		else if (Att_`j'_`p'_pv[`item', `scale']>0.05) & (Att_`j'_`p'_pv[`item', `scale']<=0.10) {
			local star_`j'_`p'_`item'`scale' %1s "*"
		}
		else {
			local star_`j'_`p'_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop
}  // foreach `p' loop


} // forvalues `j' loop





file write `hh' "\midrule"_newline
file write `hh' "Dependent Variable & \multicolumn{3}{c}{Attrition Status across Outcome Measures}  \\"_newline

file write `hh' "\cmidrule{2-4}             & DT Score   & PTSII-C Score\textsuperscript{b} & RSES/CPCS Index\textsuperscript{c}   \\" _newline
file write `hh'  "& (1) & (2) & (3)    \\\midrule\midrule" _newline
file write `hh' "Treatment           &"  %04.3f (Att_DT_score_post_mean[1,1]) `star_DT_score_post_11'   "&"  %04.3f (Att_pts_post_mean[1,1]) `star_pts_post_11'   "&"  %04.3f (Att_rosen_post_mean[1,1]) `star_rosen_post_11'    "\\" _newline
file write `hh' "         & (" %04.3f (Att_DT_score_post_se[1,1]) ")                     &(" %04.3f (Att_pts_post_se[1,1]) ") &(" %04.3f (Att_rosen_post_se[1,1]) ") \\" _newline
file write `hh' "Constant            &" %04.3f (Att_DT_score_post_mean[1,2]) `star_DT_score_post_12'  "& " %04.3f (Att_pts_post_mean[1,2]) `star_pts_post_12' "&" %04.3f (Att_rosen_post_mean[1,2]) `star_rosen_post_12' "\\" _newline
file write `hh' "      &(" %04.3f (Att_DT_score_post_se[1,2]) ")&(" %04.3f (Att_pts_post_se[1,2]) ")&(" %04.3f (Att_rosen_post_se[1,2]) ")\\" _newline
file write `hh' "       &                 &                         &                   \\\midrule" _newline
file write `hh' " Num of Obs.         &" (Att_DT_score_post_obs[1,1]) " &" (Att_pts_post_obs[1,1]) "&" (Att_rosen_post_obs[1,1]) "\\" _newline
file write `hh' "R-squared           &" %04.3f (Att_DT_score_post_r2[1,1]) "& " %04.3f (Att_pts_post_r2[1,1]) "& " %04.3f (Att_rosen_post_r2[1,1]) "\\\midrule" _newline
file write `hh' "\\" _newline


file write `hh' "\midrule\bottomrule" _newline



file write `hh' "\end{tabular}" _newline
file write `hh' "\begin{tablenotes}" _newline
file write `hh' "\item Notes: Asymptotic standard errors are shown in parentheses and are clustered at the school level. The superscripts, ***, **, *, denote the statistical significance obtained by clustered wild bootstrap-t procedures at the 1 percent, 5 percent, and 10 percent level, respectively." _newline

file write `hh' "\item \textsuperscript{a}: DT Score per min stands for math Diagnostic Test scores per minute."_newline
file write `hh' "\item \textsuperscript{b}: PTSII-C Score stands for the math proficiency test scores."_newline
file write `hh' "\item \textsuperscript{c}: The Proficiency Test of Self Learning is based on 27 survey questions, of which ten are consistent with the Children's Perceived Competence Scale (CPCS Index) and eight with the Rosenberg Self-Esteem Scale (RSES Index). For each of the non-cognitive type questions, see Appendix~\ref{AppB}." _newline
file write `hh' "\end{tablenotes}" _newline
file write `hh' "\end{threeparttable}" _newline
file write `hh' "}" _newline
file write `hh' "\label{tab:addlabel}%" _newline
file write `hh' "\end{table}" _newline

file write `hh' "" _newline
file write `hh' "" _newline
file write `hh' "" _newline
file write `hh' "" _newline

file close `hh'



	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
